import { observable, action } from 'mobx';
import * as Apis from '../../apis/Api'
export class ApiGroupShowStore {
    @observable requestParam = "false";
    @observable groups = [];//菜单分组
    @observable module = "";
    @observable info = [];
    @observable apiDataList = [];
    @observable groupId = "";
    @observable msgCode = "";
    @observable isSuccess = false;
    //多接口页面获取菜单
    @action
    getApiMenu = (props) => {
        if (props.location) {//通过链接
            this.requestParam = props.location.query;
            this.module = props.params.module;
        } else {//通过组件
            this.module = props.module;
            this.requestParam = props.requestPara;
        }

        Apis.getMenu({ module: this.module }).then(data => {
            if (data.status == "success") {
                this.groups = data.data;
                this.groupId = data.default_group;
                this.InitGroupInfo(data.default_group);
                this.isSuccess = true;
            } else {
                this.msgCode = data.msgcode;
            }
        })
    }

    /**下面处理多接口页面按分组加载 **/
    //点击子菜单加载下面的数据
    @action
    InitGroupInfo = (groupId, firstId) => {
        this.info = [];
        this.groupId = groupId;
        Apis.getConditionByGroup({ module: this.module, groupId: groupId, firstId: firstId }).then(data => {
            if (data.status == "success") {
                this.info = data.data;
                this.apiDataList = data.apiDataList;
                this.isSuccess = true;
            } else {
                this.msgCode = data.msgcode;
            }
        });
    }

    //在A菜单中点击B菜单的子菜单项,先加载B组全组的数据，再将点击的子菜单放到第一项
    @action
    onClickMenuItem = (id) => {
        Apis.getGroupIdByApifront({ id: id }).then(data => {
            if (data.status == "success") {
                this.groupId = data.groupId;
                this.InitGroupInfo(data.groupId, id);
                this.menuToTop(id);
                this.isSuccess = true;
            } else {
                this.msgCode = data.msgcode;
            }
        });
    }

    //刷新分组后的数量
    @action
    reFresh = (id, total) => {
        this.groups.map(group => {
            group.item.map(item => {
                if (id == item.itemId) {
                    item.number = total;
                }
            });
        });
    }
    //将跨分组点击后的菜单挪到第一位
    @action
    menuToTop = (id) => {
        this.groups.map(group => {
            group.item.map((item, index) => {
                if (id == item.itemId) {
                    let temp = group.item[0];
                    group.item[0] = group.item[index];
                    group.item[index] = temp;
                }
            });
        });
    }
}